<%@page import="com.glowdayz.common.PatternChecker"%>
<%@page import="com.glowdayz.glowmee.dao.CommonCodeDAO"%>
<%@page import="com.glowdayz.common.MultipartBox.FileInfo"%>
<%@page import="com.glowdayz.common.MultipartBox"%>
<%@page import="com.glowdayz.common.DBHandler"%>
<%@page import="com.glowdayz.common.ApiHandler"%>
<%@page import="com.glowdayz.common.ApiHandler.XmlData"%>
<%@page import="com.glowdayz.glowmee.cmd.RegisterCMD"%>
<%@page import="com.glowdayz.glowmee.dao.RegisterDAO"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%
	DBHandler.getInstance().connect();
	ApiHandler web = new ApiHandler(request, out, "JSPglowmee");
	
	MultipartBox mbox = MultipartBox.getInstance();
	
	// 업로드는 이 한 줄로 끝~!!!
	FileInfo finfo = mbox.upload2(request);
	
	int id = 0;
	String genderText, birthYearText, skinTypeText, locationText;
	PatternChecker checker = PatternChecker.getInstance();
	if (finfo == null) {
		id = web.getInt("id");
		genderText = web.getValue("gender");
		birthYearText = web.getValue("birthYear");
		skinTypeText = web.getValue("skinType");
		locationText = web.getValue("location");
	} else {
		id = mbox.getInt("id");
		genderText = mbox.getValue("gender");
		birthYearText = mbox.getValue("birthYear");
		skinTypeText = mbox.getValue("skinType");
		locationText = mbox.getValue("location");
	}
	
	if (!checker.isValue(genderText) || !checker.isValue(birthYearText) || !checker.isValue(skinTypeText) || !checker.isValue(locationText)) {
		DBHandler.getInstance().disconnect();
		web.result(null, "추가정보의 필수항목이 누락되었습니다.");
		return;
	}
	
	int gender = CommonCodeDAO.getInstance().getCommon("'" + genderText + "'");
	int birthYear = CommonCodeDAO.getInstance().getCommon("'" + birthYearText + "'");
	int skinType = CommonCodeDAO.getInstance().getCommon("'" + skinTypeText + "'");
	String[] temp = locationText.split(" / ");
	int location = CommonCodeDAO.getInstance().getCommon("'" + temp[1].trim() 
			+ "' AND parentId=(SELECT idCommon FROM commoncode WHERE commonText='" + temp[0].trim() + "')");
	
	RegisterCMD cmd = new RegisterCMD(id, location, birthYear, skinType, gender);
	RegisterDAO dao = new RegisterDAO();
	int update = dao.insertAddRegister(cmd, finfo);
	
	if (update < 1) {
		DBHandler.getInstance().disconnect();
		web.result(null, "수정에 실패하였습니다.");
		return;
	}
	
	DBHandler.getInstance().disconnect();
	
	ArrayList<XmlData> list = new ArrayList<XmlData>();
	list.add(new XmlData("seq", id));
	web.result("SUCCESS", "회원정보가 수정되었습니다.", list);
%>